/*
 * DxFile_test.cpp
 *
 *  Created on: Nov 14, 2011
 *      Author: yi
 */

#include "mbuild/DxFile.h"
#include <iostream>
using namespace std;

int test_read(DxFile &dxfile,char* fname) {
	//Read from file;
	try {
	dxfile.read(fname);
	} catch(FileNotOpen& error) {
		cout<<"File "<<fname<<" cannot be opened!\n";
	return 0;
	}
	return 1;
}
int test_ave(DxFile &dxfile,char* output) {
	try {
		dxfile.ave(output);
	} catch(FileNotOpen* error) {
		cout<<"No enough disk space for writting!\n";
		return 0;
	} catch(EmptyDxFile& error ) {
		cout<<"DxFile empty.\n";
		return 0;
	}
	return 1;
}
int test_rave(DxFile &dxfile,char* output) {
	try {
		dxfile.rave(output);
	} catch(FileNotOpen* error) {
		cout<<"No enough disk space for writting!\n";
		return 0;
	} catch(EmptyDxFile& error ) {
		cout<<"DxFile empty.\n";
		return 0;
	} catch(XYNotEqual& error) {
		cout<<"X & Y axis have different spacing!\n";
	    return 0;
	}
	return 1;
}
int test_write(DxFile &dxfile,char* fname) {
	//write to file;
	try{
		dxfile.write(fname);
	} catch(EmptyDxFile& error) {
		cout<<"DxFile empty.\n";
		return 0;
	} catch(FileNotOpen& error) {
		cout<<"No enough disk space for writting!\n";
		return 0;
	}
	return 1;
}

int main(void) {
	DxFile dxfile;
	char* fname="r10k10.dx";
	//TODO: test reading a Dxfile.
	cout<<"Reading "<<fname<<" into DxFile...\n";
	while (!test_read(dxfile,fname)) {
		cout<<"Try another filename:";
		cin>>fname;
	}
	//TODO: test average
	cout<<"Write average into r10k10.ave.\n";
	test_ave(dxfile,"r10k10.ave");
	cout<<"Write average into r10k10.rave \n";
	test_rave(dxfile,"r10k10.rave");
	//TODO: test writing a Dxfile.
	// out.dx should be exactly the same as r10k10.dx.
	cout<<"Write DxFile to out.dx\n";
	cout<<"Check if out.dx is exactly the same as"<<fname<<endl;
	test_write(dxfile,"out.dx");
	return 0;
}
